﻿Imports System.IO
Imports System.Xml
Imports System.Text

Public Class BlockOneHomeworkSectionFive
    Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim ArrayMax As Integer = 12
    Dim intResponse, intResponse2, intResponse3 As Integer
    Dim strUser As String = "Enter Last Name"
    Dim intChoiceACount As Integer = 2
    Dim intChoiceBCount As Integer = 3
    Dim intChoiceCCount As Integer = 4
    Dim intChoiceDCount As Integer = 5

    Shared Property MidiParent As MainForm

    Private Sub CloseToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub BlockOneSectionFive_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        txtObj.Text = "Objectives:" + vbCrLf + "- Given proper student instructional materials, identify basic facts or technical publications."
        If My.Settings.B1S5QC = 1 Then
            strUser = InputBox("Please enter last name", strUser)
            If strUser Is "" Then
                Me.Close()
            End If
        End If

        If My.Settings.B1S5QC > ArrayMax Then My.Settings.B1S5QC = ArrayMax

        If My.Settings.B1S5QC > 1 And My.Settings.B1S5QC < ArrayMax Then
            intResponse2 = MsgBox(" This assignment is already in progress. Do you want to continue?", 4, "Assignment in Progress")
            If intResponse2 = MsgBoxResult.Yes Then
                My.Settings.B1S5QC = My.Settings.B1S5QC - 1
                'Do Nothing
            ElseIf intResponse2 = MsgBoxResult.No Then
                intResponse = MsgBox("Do you want to start this over from the beginning?", 4, "Start back at the beginning?")
                If intResponse = MsgBoxResult.Yes Then
                    My.Settings.B1S5QC = 1
                ElseIf intResponse = MsgBoxResult.No Then
                    Me.Close()
                End If
            End If
        End If

        If My.Settings.B1S5QC = ArrayMax Then
            intResponse3 = MsgBox("This assignment has already been completed.  Are you sure you want to delete the current assignment and reaccomplish it?", 4, "Confirm Assignment Deletion")
            If intResponse3 = MsgBoxResult.Yes Then
                System.IO.File.Delete(filename & "\Homework\Block 1\Block 1 Section 5.txt")
                System.IO.File.Create(filename & "\Homework\Block 1\Block 1 Section 5.txt").Dispose()
                My.Settings.B1S5QC = 1
            ElseIf intResponse3 = MsgBoxResult.No Then
                Me.Close()
            End If
        End If
        chkboxChoiceA.Visible = False
        chkboxChoiceB.Visible = False
        chkboxChoiceC.Visible = False
        chkboxChoiceD.Visible = False
        GetXMLData()
        Next_Question()

    End Sub

    Private Sub btnNextQuestion_Click(sender As System.Object, e As System.EventArgs) Handles btnNextQuestion.Click
        Next_Question()
    End Sub

    Private Sub Clear_Display()
        txtQuestion.Clear()
        txtAnswer.Clear()
    End Sub

    Private Sub Clear_MultipleDisplay()
        txtQuestion.Clear()
        txtChoiceA.Clear()
        txtChoiceB.Clear()
        txtChoiceC.Clear()
        txtChoiceD.Clear()
    End Sub

    Public Function Next_Question()

        If btnNextQuestion.Text = "Done" Then
            Me.Close()
            OpenResults()
        End If
        My.Settings.B1S5QC = My.Settings.B1S5QC + 1
        If My.Settings.B1S5QC = 12 Then
            btnNextQuestion.Text = "Done"
        End If
        If My.Settings.B1S5QC = 3 Or My.Settings.B1S5QC = 7 Then
            txtAnswer.Visible = False
            lblAnswer.Visible = False
            txtChoiceA.Visible = True
            txtChoiceB.Visible = True
            txtChoiceC.Visible = True
            txtChoiceD.Visible = True
            lblChoiceA.Visible = True
            lblChoiceB.Visible = True
            lblChoiceC.Visible = True
            lblChoiceD.Visible = True
            chkboxChoiceA.Visible = True
            chkboxChoiceB.Visible = True
            chkboxChoiceC.Visible = True
            chkboxChoiceD.Visible = True
            If My.Settings.B1S5QC = 3 Then
                txtQuestion.Text = MainForm.strB1S5Q(3)
                txtChoiceA.Text = MainForm.strB1S5Q3CA
                txtChoiceB.Text = MainForm.strB1S5Q3CB
                txtChoiceC.Text = MainForm.strB1S5Q3CC
                txtChoiceD.Text = MainForm.strB1S5Q3CD
            ElseIf My.Settings.B1S5QC = 7 Then
                txtQuestion.Text = MainForm.strB1S5Q(7)
                txtChoiceA.Text = MainForm.strB1S5Q7CA
                txtChoiceB.Text = MainForm.strB1S5Q7CB
                txtChoiceC.Text = MainForm.strB1S5Q7CC
                txtChoiceD.Text = MainForm.strB1S5Q7CD

            ElseIf My.Settings.B1S5QC = 0 Or My.Settings.B1S5QC = 1 Or My.Settings.B1S5QC = 2 _
                Or My.Settings.B1S5QC = 4 Or My.Settings.B1S5QC = 5 Or My.Settings.B1S5QC = 6 _
                Or My.Settings.B1S5QC = 8 Or My.Settings.B1S5QC = 9 Or My.Settings.B1S5QC = 10 _
                Or My.Settings.B1S5QC = 11 Or My.Settings.B1S5QC = 12 Then
                txtAnswer.Visible = True
                lblAnswer.Visible = True
                txtChoiceA.Visible = False
                txtChoiceB.Visible = False
                txtChoiceC.Visible = False
                txtChoiceD.Visible = False
                lblChoiceA.Visible = False
                lblChoiceB.Visible = False
                lblChoiceC.Visible = False
                lblChoiceD.Visible = False
                chkboxChoiceA.Visible = False
                chkboxChoiceB.Visible = False
                chkboxChoiceC.Visible = False
                chkboxChoiceD.Visible = False
            End If

            txtAnswer.Focus()

            If btnNextQuestion.Text = "Done" Then
                AddXMLData()
                WriteToFile()
                Me.Close()
                OpenResults()
            End If

            If My.Settings.B1S5QC = ArrayMax Then '**** Linus 12 July ***
                btnNextQuestion.Text = "Done"
            End If

            If My.Settings.B1S5QC <= ArrayMax Then '**** Linus 12 July ****
                txtQuestion.Text = MainForm.strB1S5Q(My.Settings.B1S5QC)
                txtAnswer.Text = MainForm.strB1S5A(My.Settings.B1S5QC)
            End If
        End If
        Return My.Settings.B1S5QC
    End Function

    Private Sub WriteToFile()
        Dim Integrity As String = (filename) & "\Instructor\Block 1\Block 1 Section 5.txt"
        Dim Results As String = (filename) & "\Homework\Block 1\Block 1 Section 5.txt"
        '***********Output to hidden file for Instructor Check***********
        If System.IO.File.Exists(Integrity) = True Then
            Dim hiddenWriter As New System.IO.StreamWriter(Integrity, True)
            If My.Settings.B1S5QC = 1 Then
                hiddenWriter.WriteLine("- " & txtObj.Text)
                hiddenWriter.WriteLine()
                hiddenWriter.WriteLine("Student: " & strUser)
                hiddenWriter.WriteLine()
                hiddenWriter.WriteLine("Date: " & (DateValue(Now)))
                hiddenWriter.WriteLine("Time: " & (TimeValue(Now)))
            End If
            hiddenWriter.WriteLine("Question: " & txtQuestion.Text)
            If My.Settings.B1S5QC = 3 Or My.Settings.B1S5QC = 7 Then
                If chkboxChoiceA.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  A: " & txtChoiceA.Text)
                ElseIf chkboxChoiceB.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  B: " & txtChoiceB.Text)
                ElseIf chkboxChoiceC.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  C: " & txtChoiceC.Text)
                ElseIf chkboxChoiceD.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  D: " & txtChoiceD.Text)
                End If
                hiddenWriter.WriteLine("Reference: " & txtReference.Text)
                hiddenWriter.WriteLine()
            End If

            If chkboxChoiceA.Visible = False Then
                hiddenWriter.WriteLine("Answer: " & txtAnswer.Text)
                hiddenWriter.WriteLine("Reference: " & txtReference.Text)
                hiddenWriter.WriteLine()
            End If

            If My.Settings.B1S5QC = 12 Then
                hiddenWriter.WriteLine("Block 1 Section 5 Complete")
                hiddenWriter.WriteLine("Date: " & (DateValue(Now)))
                hiddenWriter.WriteLine("Time: " & (TimeValue(Now)))
            End If
            hiddenWriter.Close()

            'MsgBox("Text written to file")
        Else
            MsgBox("File Does Not Exist PRIVATE")
        End If

        '******Output to file for Student to review***********
        If System.IO.File.Exists(Results) = True Then
            Dim hiddenWriter As New System.IO.StreamWriter(Results, True)
            If My.Settings.B1S5QC = 1 Then
                hiddenWriter.WriteLine("- " & txtObj.Text)
                hiddenWriter.WriteLine()
                hiddenWriter.WriteLine("Student: " & strUser)
                hiddenWriter.WriteLine()
            End If
            hiddenWriter.WriteLine("Question: " & txtQuestion.Text)
            If My.Settings.B1S5QC = 3 Or My.Settings.B1S5QC = 7 Then
                If chkboxChoiceA.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  A: " & txtChoiceA.Text)
                ElseIf chkboxChoiceB.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  B: " & txtChoiceB.Text)
                ElseIf chkboxChoiceC.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  C: " & txtChoiceC.Text)
                ElseIf chkboxChoiceD.CheckState = 1 Then
                    hiddenWriter.WriteLine("Answer:  D: " & txtChoiceD.Text)
                End If
                hiddenWriter.WriteLine()
            End If
            If chkboxChoiceA.Visible = False Then
                hiddenWriter.WriteLine("Answer: " & txtAnswer.Text)
                hiddenWriter.WriteLine()
            End If
            If My.Settings.B1S5QC = 12 Then
                hiddenWriter.WriteLine("Block 1 Section 5 Complete")
            End If
            hiddenWriter.Close()
        Else
            MsgBox("File Does Not Exist")
        End If
        If My.Settings.B1S5QC > 0 Then
            txtAnswer.Clear()
            chkboxChoiceA.CheckState = CheckState.Unchecked
            chkboxChoiceB.CheckState = CheckState.Unchecked
            chkboxChoiceC.CheckState = CheckState.Unchecked
            chkboxChoiceD.CheckState = CheckState.Unchecked
        End If
    End Sub

    Private Sub OpenResults()
        Dim p As Process = Process.Start((filename) & "\Homework\Block 1\Block 1 Section 5.txt")
    End Sub

    Private Sub btnPreviousQuestion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousQuestion.Click

        If My.Settings.B1S5QC = 0 Then Return '***** Linus 11 July *****

        If My.Settings.B1S5QC = ArrayMax Then '**** Linus 12 July *** Then
            btnNextQuestion.Text = "Next Question"
        End If

        My.Settings.B1S5QC = My.Settings.B1S5QC - 1

        txtQuestion.Text = MainForm.strB1S5Q(My.Settings.B1S5QC)
        txtAnswer.Text = MainForm.strB1S5A(My.Settings.B1S5QC) ' **** Linus 12 July ****
        txtAnswer.Focus()

    End Sub

    Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtAnswer.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            MainForm.strB1S5A(My.Settings.B1S5QC) = txtAnswer.Text
            My.Settings.B1S5QC = My.Settings.B1S5QC + 1
            Next_Question()
        End If
    End Sub

    Private Sub BlockOneHomeworkSectionOne_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        My.Settings.Save()
    End Sub

    Private Sub txtAnswer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAnswer.TextChanged
        txtAnswer.Text = txtAnswer.Text.Trim
    End Sub

    Public Sub GetXMLData()
        Dim xmlReader As New XmlTextReader("C:\Temp\B1S5AFSData.xml")

        While xmlReader.Read()
            For i = 0 To 12
                If xmlReader.Name.ToString() = "Answer" & i Then
                    MainForm.strB1S5A(i) = xmlReader.ReadString().Replace("None", "")
                End If
                If xmlReader.Name.ToString() = "Question" & i Then
                    MainForm.strB1S5Q(i) = xmlReader.ReadString()
                End If
                If xmlReader.Name.ToString() = "ChoiceA" Then
                    MainForm.strB1S5Q3CA = xmlReader.ReadString()
                ElseIf xmlReader.Name.ToString() = "ChoiceB then" Then
                    MainForm.strB1S5Q3CB = xmlReader.ReadString()
                ElseIf xmlReader.Name.ToString() = "ChoiceC then" Then
                    MainForm.strB1S5Q3CC = xmlReader.ReadString()
                ElseIf xmlReader.Name.ToString() = "ChoiceD then" Then
                    MainForm.strB1S5Q3CD = xmlReader.ReadString()
                End If
                If xmlReader.Name.ToString() = "ChoiceA" Then
                    MainForm.strB1S5Q7CA = xmlReader.ReadString()
                ElseIf xmlReader.Name.ToString() = "ChoiceB then" Then
                    MainForm.strB1S5Q7CB = xmlReader.ReadString()
                ElseIf xmlReader.Name.ToString() = "ChoiceC then" Then
                    MainForm.strB1S5Q7CC = xmlReader.ReadString()
                ElseIf xmlReader.Name.ToString() = "ChoiceD then" Then
                    MainForm.strB1S5Q7CD = xmlReader.ReadString()
                End If
            Next
        End While
        xmlReader.Close()
    End Sub

    Private Sub AddXMLData()
        Dim xd As New XmlDocument
        xd.Load("C:\Temp\B1S5AFSData.xml")
        Dim nod1 As XmlElement = xd.DocumentElement
        Dim nod2 As XmlElement = xd.DocumentElement
        Dim nod3 As XmlElement = xd.DocumentElement
        Dim nod4 As XmlElement = xd.DocumentElement
        Dim nod5 As XmlElement = xd.DocumentElement
        Dim nod6 As XmlElement = xd.DocumentElement
        Dim nod7 As XmlElement = xd.DocumentElement
        Dim nod8 As XmlElement = xd.DocumentElement
        Dim nod9 As XmlElement = xd.DocumentElement
        Dim nod30 As XmlElement = xd.DocumentElement
        Dim nod31 As XmlElement = xd.DocumentElement
        Dim nod32 As XmlElement = xd.DocumentElement

        Dim nod10, nod11, nod12, nod13, nod14, nod15, nod16, nod17, nod18, nod19 As XmlNode

        nod10 = nod1.SelectSingleNode("Answer1")
        nod10.InnerText = MainForm.strB1S5A(1)
        nod11 = nod2.SelectSingleNode("Answer2")
        nod11.InnerText = MainForm.strB1S5A(2)
        nod12 = nod3.SelectSingleNode("Answer4")
        nod12.InnerText = MainForm.strB1S5A(4)
        nod13 = nod4.SelectSingleNode("Answer5")
        nod13.InnerText = MainForm.strB1S5A(5)
        nod14 = nod5.SelectSingleNode("Answer6")
        nod14.InnerText = MainForm.strB1S5A(6)
        nod15 = nod6.SelectSingleNode("Answer8")
        nod15.InnerText = MainForm.strB1S5A(8)
        nod16 = nod7.SelectSingleNode("Answer9")
        nod16.InnerText = MainForm.strB1S5A(9)
        nod17 = nod8.SelectSingleNode("Answer10")
        nod17.InnerText = MainForm.strB1S5A(10)
        nod18 = nod9.SelectSingleNode("Answer11")
        nod18.InnerText = MainForm.strB1S5A(11)
        nod19 = nod10.SelectSingleNode("Answer12")
        nod19.InnerText = MainForm.strB1S5A(12)

        xd.Save("C:\Temp\B1S5AFSData.xml")
    End Sub
End Class


